package com.jxc.summer.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jxc.summer.service.CompanyService;
import com.jxc.summer.util.MD5Util;
import com.jxc.summer.util.ReturnT;
import com.jxc.summer.util.UUIDUtil;

/**
 * <p>Title: CompanyController<／p>
 * @author 耿瑞阳
 * @date 2017年5月8日 下午9:56:08
 */
@Controller
@RequestMapping("/companyinfo")
public class CompanyController {

	@Autowired
	private CompanyService companyService;
	
	@RequestMapping
	public String index(Model model) {
		return "company/company.index";
	}
	
	@RequestMapping("/pageList")
	@ResponseBody
	public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int start,  
			@RequestParam(required = false, defaultValue = "10") int length,@RequestParam(required = false, defaultValue = "") String companyName) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("companyName", companyName);
		params.put("offset", start);
		params.put("pagesize", length);
		Map<String, Object> result = companyService.pageList(params);
		return result;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public ReturnT<String> addAccount(@RequestParam(required = true) String companyName,  
			@RequestParam(required = true) String companyCode) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_id",UUIDUtil.get32UUID());
		params.put("tc_name",companyName);
		params.put("tc_code",companyCode);
		if(!companyService.validateCompanyName(params)){
			return new ReturnT<String>(500, "新增商户失败！商户名已存在。");
		}
		if(!companyService.validateCompanyCode(params)){
			return new ReturnT<String>(500, "新增商户失败！商户Code已存在。");
		}
		int result = companyService.addCompany(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "新增账户失败！");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public ReturnT<String> updateCompany(@RequestParam(required = true) String id,@RequestParam(required = true) String companyName,  
			@RequestParam(required = true) String companyCode){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_id", id);
		params.put("tc_name", companyName);
		params.put("tc_code", companyCode);
		if(!companyService.validateCompanyName(params)){
			return new ReturnT<String>(500, "更新商户失败！商户名已存在。");
		}
		if(!companyService.validateCompanyCode(params)){
			return new ReturnT<String>(500, "更新商户失败！商户Code已存在。");
		}
		int result = companyService.updateCompany(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "更新账户失败！");
		}
	}
	
}
